import {request} from '@/utils/request'
import type { HbtPagedResult, HbtSelectOption } from '@/types/common'
import type { 
  HbtRoleQuery, 
  HbtRole,
  HbtRoleCreate,
  HbtRoleUpdate,
  HbtRoleStatus
} from '@/types/identity/role'

/**
 * 获取角色分页列表
 */
export function getRoleList(params: HbtRoleQuery) {
  return request<HbtPagedResult<HbtRole>>({
    url: '/api/HbtRole/list',
    method: 'get',
    params
  })
}

/**
 * 获取角色详情
 */
export function getByIdAsync(roleId: string) {
  return request<HbtRole>({
    url: `/api/HbtRole/${roleId}`,
    method: 'get'
  })
}

/**
 * 创建角色
 */
export function createRole(data: HbtRoleCreate) {
  return request<number>({
    url: '/api/HbtRole',
    method: 'post',
    data
  })
}

/**
 * 更新角色
 */
export function updateRole(data: HbtRoleUpdate) {
  return request<boolean>({
    url: '/api/HbtRole',
    method: 'put',
    data
  })
}

/**
 * 删除角色
 */
export function deleteRole(roleId: string) {
  return request<boolean>({
    url: `/api/HbtRole/${roleId}`,
    method: 'delete'
  })
}

/**
 * 批量删除角色
 */
export function batchDeleteRole(roleIds: string[]) {
  return request<boolean>({
    url: '/api/HbtRole/batch',
    method: 'delete',
    data: roleIds
  })
}

/**
 * 更新角色状态
 */
export function updateRoleStatus(data: HbtRoleStatus) {
  return request<boolean>({
    url: `/api/HbtRole/${data.roleId}/status`,
    method: 'put',
    params: { status: data.status }
  })
}

/**
 * 获取角色选项列表
 */
export function getRoleOptions() {
  return request<HbtSelectOption[]>({
    url: '/api/HbtRole/options',
    method: 'get'
  })
}

/**
 * 获取角色部门列表
 */
export function getRoleDepts(roleId: string) {
  return request<any[]>({
    url: `/api/HbtRole/${roleId}/depts`,
    method: 'get'
  })
}

/**
 * 获取角色菜单列表
 */
export function getRoleMenus(roleId: string) {
  return request<any[]>({
    url: `/api/HbtRole/${roleId}/menus`,
    method: 'get'
  })
}

/**
 * 分配角色菜单
 */
export function assignRoleMenus(roleId: string, menuIds: string[]) {
  return request<boolean>({
    url: `/api/HbtRole/${roleId}/menus`,
    method: 'put',
    data: menuIds
  })
}

/**
 * 分配角色用户
 */
export function assignRoleUsers(roleId: string, userIds: string[]) {
  return request<boolean>({
    url: `/api/HbtRole/${roleId}/users`,
    method: 'put',
    data: userIds
  })
}

/**
 * 分配角色部门
 */
export function assignRoleDepts(roleId: string, deptIds: string[]) {
  return request<boolean>({
    url: `/api/HbtRole/${roleId}/depts`,
    method: 'put',
    data: deptIds
  })
}

/**
 * 导入角色数据
 */
export function importRole(file: File) {
  const formData = new FormData()
  formData.append('file', file)
  return request<{ success: number; fail: number }>({
    url: '/api/HbtRole/import',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

/**
 * 导出角色数据
 */
export function exportRole(params?: HbtRoleQuery) {
  return request({
    url: '/api/HbtRole/export',
    method: 'get',
    params,
    responseType: 'blob'
  })
}

/**
 * 获取角色导入模板
 */
export function getTemplate() {
  return request({
    url: '/api/HbtRole/template',
    method: 'get',
    responseType: 'blob'
  })
}
